﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Essentials;


using Accouting804.Models.Orm;
using System.IO;
using System.Reflection;

using SQLitePCL;
using Accouting804.Setting;
using Plugin.FilePicker;
using Plugin.FilePicker.Abstractions;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Accouting804.ViewModels.AccountViewModels;
using System.Diagnostics;

namespace Accouting804.Views
{


    [XamlCompilation(XamlCompilationOptions.Compile)]
    [DesignTimeVisible(false)]
    public partial class AccountPage : ContentPage
    {
        AccountItemsViewModel viewModel = new AccountItemsViewModel();
        public ObservableCollection<AccountRecord> AccountRecords { get; set; }

        public AccountPage()
        {
            InitializeComponent();
            BindingContext = viewModel;

        }
        public const string DatabaseFilename = "db.db3";
        //private ItemsViewModel viewModel;

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void ToolbarItem_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new NavigationPage(new NewAccountItemPage()));
            //await Navigation.PopModalAsync();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            if (viewModel.Items.Count == 0)
                viewModel.LoadItemsCommand.Execute(null);
        }

        private async void filter_btn_Clicked(object sender, EventArgs e)
        {
            var filterAndSorterPage = new FilterAndSorterPage();
            filterAndSorterPage.Confirmed += (vm) =>
            {



            };
            await Navigation.PushModalAsync(new NavigationPage(filterAndSorterPage));


        }

        private void refresh_btn_Clicked(object sender, EventArgs e)
        {

            ItemsListView.BeginRefresh();
            ItemsListView.EndRefresh();
        }

        public void Refresh()
        {
            this.ItemsListView.BeginRefresh();
            this.ItemsListView.EndRefresh();

        }

        /// <summary>
        /// 处理listview某个元素点击
        /// </summary>
        /// <param name="sender">发送者，listview实例</param>
        /// <param name="args">参数，其SelectedItem为accountRecord实例</param>
        async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
        {
            var item = args.SelectedItem as AccountRecord;

            if (item == null) { return; }
            var detailPage = new AccountItemDetailPage(new SingleAccoutViewModel()
            {
                Item = item,
                SingleAccountViewModelType = SingleAccountViewModelType.Edit,
            });
            detailPage.Disappearing += (a, b) =>
            {
                Refresh();
                ;
            };
            await Navigation.PushModalAsync(new NavigationPage(detailPage));

            //取消ItemsListView的选中，否则退出再点没反应
            ItemsListView.SelectedItem = null;
        }
    }






}